package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.ErrorBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.Error;
import com.liuzu.esys.service.ErrorService;
import com.liuzu.esys.vo.YyhBarVo;
import com.liuzu.esys.vo.YyhPieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Description :天干物燥，小心火烛
 * @Author : yyh
 * @Date : 2022/07/25 15:08:06
 * @desc
 */
@RestController
@RequestMapping("/error")
public class ErrorController {
    @Autowired
    private ErrorService errorService;

    //分页和模糊查询的接口
    //     *  params 接受 分页参数  和  模糊查询参数
    @RequestMapping("/errorList")
    public LayuiPageVo<Error> doctorPageData(@RequestParam Map<String, Object> params) {
        return errorService.errorPageData(params);
    }

    //getMapping会匹配预检请求
    @GetMapping("/deleteError")
    public R deleteError(Integer id) {
        int result = errorService.deleteError(id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/updateError")
    public R updateError(Error error) {
        int result = errorService.updateError(error);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/addError")
    public R addError(Error error) {
        int result = errorService.addError(error);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //柱状失误级别信息图
    @RequestMapping("/echartBar")
    public R echartBar() {
        List<YyhBarVo> list = errorService.levelBarData();
        return R.ok().data("levelBar", list);
    }

    //饼状失误级别信息图
    @RequestMapping("/echartsPie")
    public R echartsPie() {

        List<YyhPieVo> list = errorService.punishPieData();

        return R.ok().data("punishPie", list);
    }

    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Error error, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("失误记录", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, ErrorBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<ErrorBo> errors = errorService.errorExcelData(error);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(errors);
    }
}
